/*
 *  Copyright (C) 2010 Glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.annotation.util;

import java.lang.reflect.Field;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.balisunrise.annotation.GridOrder;
import org.balisunrise.annotation.OrderDefinition;

/**
 *
 * @author Glauco
 */
public class OrderUtilities {

    private static final String MESSAGE = "Erro ao ler Annotation \"GridOrder\".";

    /**
     * Atributo com a anotação <code>@GridColumn</code> da classe de uma entidade.
     */
    public Field field;

    /**
     * Alias (nome) da definição.
     */
    public String alias;

    /**
     * Indice em que o atributo deve aparecer na tela.
     */
    public int index;

    /**
     *
     * @param field
     * @param alias
     * @return
     * @throws AnnotationsUtilitiesException
     */
    public static OrderDefinition readOrderAnnotation(Field field, String alias)
            throws AnnotationsUtilitiesException{

        if (field == null)
            throw new AnnotationsUtilitiesException( MESSAGE +
                    "\nParâmentro \"field\" não pode ser nulo!");

        if(alias == null)
            throw new AnnotationsUtilitiesException(MESSAGE +
                    "\nParâmentro \"alias\" não pode ser nulo!");

        if(field.isAnnotationPresent(GridOrder.class)){
            GridOrder order = field.getAnnotation(GridOrder.class);
            for (OrderDefinition od : order.order()) {
                for (String s : od.alias()) {
                    if (s.equals(alias))
                        return od;
                }
            }
        }

        throw new AnnotationsUtilitiesException(MESSAGE +
                    "\nAnnotation \"OrderDefinition\" não encontrada para o " +
                    "nome\""+ alias +"\"!");
    }

    /**
     *
     * @param field
     * @param alias
     * @return
     * @throws AnnotationsUtilitiesException
     */
    public static OrderUtilities create(Field field, String alias)
            throws AnnotationsUtilitiesException{

        if (field == null)
            throw new AnnotationsUtilitiesException( MESSAGE +
                    "\nParâmentro \"field\" não pode ser nulo!");

        if(alias == null)
            throw new AnnotationsUtilitiesException(MESSAGE +
                    "\nParâmentro \"alias\" não pode ser nulo!");

        OrderDefinition od = readOrderAnnotation(field, alias);
        
        OrderUtilities ou = new OrderUtilities();
        ou.field = field;
        ou.alias = alias;
        ou.index = od.index();

        return ou;
    }

    /**
     *
     * @param orders
     * @throws AnnotationsUtilitiesException
     */
    public static void sort(List<OrderUtilities> orders)
            throws AnnotationsUtilitiesException{

        if (orders == null)
            throw new AnnotationsUtilitiesException( MESSAGE +
                    "\nParâmentro \"list\" não pode ser nulo!");

        //Ordena a lista
        Collections.sort(orders, new Comparator<OrderUtilities>(){
            @Override
            public int compare(OrderUtilities o1,
                    OrderUtilities o2) {

                int order1 = o1.index;
                int order2 = o2.index;

                // compara
                return order1 < order2 ? -1 : (order1 > order2 ? 1 : 0);

            }
        });
    }
}
